package com.gym.Thread;
/**
 * 火车票售票窗口模拟
 * @author Administrator
 *
 */
public class TicketTest {
	public static void main(String[] args) {
		Ticket ticket = new Ticket();
		Thread thread1 = new Thread(ticket, "窗口一");
		Thread thread2 = new Thread(ticket, "窗口二");
		Thread thread3 = new Thread(ticket, "窗口三");
		Thread thread4 = new Thread(ticket, "窗口四");
		thread1.start();
		thread2.start();
		thread3.start();
		thread4.start();
	}
}
 class  Ticket  implements Runnable{
	private int ticket = 200;
	@Override
	public void run() {
		while (true) {
			synchronized (this) {
				if(ticket>0){
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					System.err.println(Thread.currentThread().getName()+"售出票"+ticket);
					ticket--;
				}else{
					break;
				}
			}
		}
	}
}
